package tw.cating.karmasaver.rss.structer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class rssNode {
	private String mUrl = null;
	private String mDesc = null;
	private String mRssFrom = null;
	
	protected static final String bitlyUsername = "cating";
	protected static final String API_KEY = "R_623163db84c572b94f47707a9e9d519d";
	
	public rssNode() {
		
	}
	
	public rssNode(String desc, String url) {
	}
	
	public void setDescription(String desc) {
		mDesc = new String(desc);
	}
	
	public void setUrl(String url) {
		mUrl = new String(url);
	}
	
	public void setFrom(String from) {
		mRssFrom = new String(from);
	}
	
	public String getDescription() {
		return mDesc;
	}
	
	public String getLink() {
		return mUrl;
	}
	
    public String convertStreamToString(InputStream is) {   
        /*  
         * To convert the InputStream to String we use the BufferedReader.readLine()  
         * method. We iterate until the BufferedReader return null which means  
         * there's no more data to read. Each line will appended to a StringBuilder  
         * and returned as String.  
         */  
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
        StringBuilder sb = new StringBuilder();   
    
        String line = null;   
        try {   
            while ((line = reader.readLine()) != null) {   
                sb.append(line + "\n");   
            }   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                is.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
    
        return sb.toString();   
    }
	
	public String getShorternLink() {
		String shorternUrl = mUrl;
		DefaultHttpClient client = new DefaultHttpClient();
		String url = "http://api.bitly.com/v3/shorten?login=" + bitlyUsername + "&apiKey=" + API_KEY + "&longUrl=" + mUrl;
		HttpGet get = new HttpGet(url);
		try {
			final HttpResponse resp = client.execute(get);
			if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String result = convertStreamToString(resp.getEntity().getContent());
				JSONObject shortern = new JSONObject(result);
				if (shortern.has("data")) {
					JSONObject data = shortern.getJSONObject("data");
					if (data.has("url"))
						shorternUrl = data.getString("url"); 
				}
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shorternUrl;
	}
	
	public String getFrom() {
		return mRssFrom;
	}
}
